package com.ihg.hiex.admin.handler.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ihg.hiex.admin.entity.upload.OnlineAccount;
import com.ihg.hiex.admin.handler.OnlineAccountUploadHandler;

/**
 * 
 * @author dan
 *
 */
@Component("onlineAccountUploadHandlerImpl")
public class OnlineAccountUploadHandlerImpl implements OnlineAccountUploadHandler{
    
    private static final Logger logger = LoggerFactory.getLogger(OnlineAccountUploadHandlerImpl.class);

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.handler.OnlineAccountUploadHandler#upload(java.io.InputStream)
     */
    @SuppressWarnings("unchecked")
    public List<OnlineAccount> upload(InputStream inputStream) {
        List<OnlineAccount> onlineAccounts = (List<OnlineAccount>) this.parse(inputStream, true);
        return onlineAccounts;
    }
    
    /**
     * 
     * @param inputStream
     * @param isAssessor
     * @return
     */
    private Object parse(InputStream inputStream, boolean isAssessor) {
        
        // 0. prepare data
        List<Object> result = new ArrayList<Object>();
        DataFormatter formatter = new DataFormatter();
        
        try {
            
            logger.trace("#parse() - Starting parsing upload file.");

            // 1. get workbook
            Workbook workbook = WorkbookFactory.create(inputStream);
            
            // 2. get sheet and rows
            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rows = sheet.iterator();
            
            while (rows.hasNext()) {
                
                Row row = rows.next();
                int rowNum = row.getRowNum();
                
                if (rowNum <= 1) {
                    continue;
                }
                
                OnlineAccount onlineAccount = this.initOnlineAccount(formatter, row);
                if(onlineAccount != null) {
                    result.add(onlineAccount);
                }
            }
            
            logger.trace("#parse() - Finishing parsing file with {} records.", result.size());
            
        } catch (Exception e) {
            logger.error("#parse() - Finishing parsing file with errors.", e);
        }
        
        return result;
        
    }
    
    /**
     * construct assessor by excel formatter
     * 
     * @param formatter
     * @param row
     * @return
     */
    private OnlineAccount initOnlineAccount(DataFormatter formatter, Row row) {
        
        // construct assessor
        String email = formatter.formatCellValue(row.getCell(0));
        String waveAccount = formatter.formatCellValue(row.getCell(2));
        String swiftAccount = formatter.formatCellValue(row.getCell(3));
        String swiftPassword = formatter.formatCellValue(row.getCell(4));
        
        if(StringUtils.isBlank(email)) {
            return null;
        } else {
            return new OnlineAccount(email, waveAccount, swiftAccount, swiftPassword);
        }
        
    }

}
